#pragma once
/************************************************************************/
/* ComPortHandler: this class handles all the stuff related to the com port                                                                     */
/************************************************************************/
#include "Utility.h"
#include "FileWriter.h"

#define  DEF_WRITECMD_TW 1000
#define  DEF_READ_TW     1000
typedef unsigned char byte;

class ComPortHandler
{
public:
	ComPortHandler(LogFileWriter& logger);
	~ComPortHandler(void);

	friend	UINT	ReadThreadFuc( LPVOID pParam );

	void			setWriteCommand(byte command);

	bool			writeCommandToComPort();

	bool			openComPort();

	void			startReadFromComPort();

	void			endReadFromComPort();

	void			closeComPortHanle();

	//query if the reading thread is working
	bool			isReadingComPort();

	bool			isComPortOpen();

	void			setDeviceID(byte deviceID );

	void			setComPortID(byte comID);

	byte			getComPortID();

	void			getRotateValueByQuat(float& rAngle, float& rX, float& rY, float& rZ);

	float*			getGyro();
	float*			getAcc();
	float*			getMag();
	float*			getQuat();
	float			getRoll();
	float			getPitch();
	float			getYaw();

private:
	void			readFromComPort();
	bool			readBytesFromComPort(void* pReadData, int numOfBytes = 1);	
	void			handleReadData(float* pData);

	
private:
	bool			mIsComPortOpen;
	byte			mWriteCommand[5];
	byte			mDeviceID;
	byte			mComPortID;
	HANDLE			m_hCom;				//handle of current com port
	bool			mIsReadingComPort;

	//here's the data:
	float			mGyro[3];
	float			mAcc[3];
	float			mMag[3];
	float			mQuat[4];
	float			mRoll;
	float			mPitch;
	float           mYaw;

	LogFileWriter&	mLogger;
	
};

